package com.yj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.yj.entity.Houses;
import com.yj.entity.Region;
import com.yj.service.IHousesService;
import com.yj.vo.JsonResult;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yj
 * @since 2020-08-24
 */
@RestController
//@RequestMapping("/house")
public class HousesController {

	@Autowired
	IHousesService houses;
	
	//查询所有房源总页码
	@PostMapping("/pages")
	public JsonResult pages() {
		return houses.pages();
	}
	
	//根据页码号查询房源信息
	@PostMapping("/page")
	public JsonResult page(@RequestParam(value = "pageNnum",defaultValue = "1") int pa) {
		return houses.page(pa);
	}
	
	@PostMapping("/index")
	public JsonResult index() {
		JsonResult ret = new JsonResult(true, "首页");
		return ret;
	}
	
	//搜索
	@PostMapping("/find")
	public JsonResult find(String info) {
		return houses.find(info);
	}

	//分类查询
	@PostMapping(value = "/findbytype", produces = "application/json;charset=UTF-8")
	public JsonResult findByType(@RequestBody JSONObject json) {
		return houses.findByType(json);
	}
	
	//推荐房源
	@PostMapping("/foundHouse")
	public JsonResult findHouse() {
		return houses.fondHouse();
	}
	

	//更新房源信息
	@PostMapping("/users/updhouse")
	public JsonResult updhouse(Houses house,Region region) {
		return houses.updhouse(house,region);

	}

	//根据hid删除房源
	@PostMapping("/users/delhouse")
	public JsonResult delhouse(int hid) {
		return houses.delhouse(hid);
	}
	
	//上传房源图片
	@PostMapping("/users/upHouseImg")
	public JsonResult upHouseImg(int hid, HttpServletRequest request) {
		return houses.upHouseImg(hid, request);
	}

	// 根据房子id查询详细房源信息
	@PostMapping("/showhousedetail")
	public JsonResult showhousedetail(int hid) {
		return houses.showhousedetail(hid);
	}

	
	

	// 发布房源信息
	@PostMapping("/users/inserthouse")
	public JsonResult inserthouse(Houses hous, Region region,HttpSession session) {
		return houses.inserthouse(hous, region,session);

	}

	// 根据登录时存的用户信息查找他发布过的房源信息
	@PostMapping("/users/findbyUid")
	public JsonResult findbyUid(HttpSession session) {
		return houses.findbyUid(session);
	}

	// 根据登录时存的用户信息查找他租过的房子的信息
	@PostMapping("/users/findzufang")
	public JsonResult findzufang(HttpSession session) {
		return houses.findzufang(session);
	}

	// 根据地区推荐同地区下的房源信息
	@PostMapping("/getdidfang")
	public JsonResult findBydid(int did) {
		return houses.findByDid(did);
	}

}
